package com.mz.ozy.activity.me;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mz.ozy.BuildConfig;
import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AboutBean;
import com.mz.ozy.mvp.presenter.ModifyPwdPresenter;
import com.mz.ozy.mvp.view.ModifyPwdContract;
import com.mz.ozy.utils.DataCleanUtils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.BaseDialog;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/3 0003.
 */
public class AboutMeActivity extends BaseActivity<ModifyPwdContract.View, ModifyPwdPresenter> implements ModifyPwdContract.View {
    @BindView(R.id.tv_clean)
    TextView tvClean;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("关于我们");
        tvClean.setText(DataCleanUtils.getTotalCacheSize(AboutMeActivity.this));
        tvVersion.setText(BuildConfig.VERSION_NAME);
        mPresenter.getAbout(this);
    }

    @OnClick({R.id.rele_xy, R.id.rele_content, R.id.rele_clear, R.id.tv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rele_content:
                CustomServiceActivity.go(this, 0);
                break;
            case R.id.rele_xy:
                CustomServiceActivity.go(this, 1);
                break;
            case R.id.tv_phone:
                final String telPhone = tvPhone.getText().toString().trim();
                if (!TextUtils.isEmpty(telPhone)) {
                    callPhone(telPhone);
                }
                break;
            case R.id.rele_clear:
                final BaseDialog dialog = new BaseDialog(AboutMeActivity.this, R.layout.dialog_normal, Gravity.CENTER, true);
                ((TextView) dialog.getView(R.id.tv_dialog_content)).setText("确认清除缓存");
                ((TextView) dialog.getView(R.id.tv_dialog_title)).setText("清除缓存");
                dialog.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.toggleDialog();
                    }
                });

                dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DataCleanUtils.clearAllCache(AboutMeActivity.this);
                        tvClean.setText(DataCleanUtils.getTotalCacheSize(AboutMeActivity.this));

                        dialog.toggleDialog();
                    }
                });
                dialog.toggleDialog();
                break;
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    @Override
    public void obtainModifyPwdSuccess(Object o) {

    }

    @Override
    public void getCodeSuccess(Object o) {

    }

    @Override
    public void getSuccess(AboutBean o) {
        GlideUtils.loadImg(this, image, o.getImg(), R.mipmap.about_logo);
        tvAddress.setText(o.getAddress());
        tvPhone.setText(o.getMobile());
        tvContent.setText(o.getContent());
    }
}
